import {
  saveOrUpdateSpread ,listSpreadByType,delSpread,listSpreadForShake,listSpreadResult ,getSpreadById
} from '../services/contentManager';
import { routerRedux } from 'dva/router';

export default {
  namespace: 'contentManager',

  state: {
    index:0,
    loading: true,
    checkUserListData:[],
  },
  effects: {
    *fetchSaveOrUpdateSpread({payload ,callback}, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(saveOrUpdateSpread,payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      callback(response)
    }
    ,
    *fetchListSpreadByType({payload,callback}, { call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(listSpreadByType,payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      callback(response.data)
    }
    ,
    *fetchDelSpread({payload,callback}, { call, put }) {
      const response = yield call(delSpread,payload);
      callback(response)
    }
    ,
    *fetchListSpreadForShake({payload,callback}, {call, put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(listSpreadForShake,payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      callback(response.data)
    }
    ,
    /**
     * 进入到摇一摇 直播 广告 的 新增 编辑 详情 页面
     * @param payload
     * @param put
     */
    *fetchLiveAdd({payload}, { put }) {
      if(payload.spreadId) {
        yield put(routerRedux.push({
          pathname:'/rdSpread/'+payload.path,
          query:{
            spreadId:payload.spreadId,
            showDetail:payload.showDetail,
          }
        }));
      }else{
        yield put(routerRedux.push({
          pathname:'/rdSpread/'+payload.path
        }));
      }

    }
    ,
    *fetchListSpreadResult({payload,callback}, { call,put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(listSpreadResult,payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      callback(response.data)
    }
    ,
    *fetchGetSpreadById({payload ,callback}, { call,put }) {
      yield put({
        type: 'changeLoading',
        payload: true,
      });
      const response = yield call(getSpreadById,payload);
      yield put({
        type: 'changeLoading',
        payload: false,
      });
      callback(response.data)
    }
  },

  reducers: {
    listSpreadByType(state, action) {
      return {
        ...state,
        spreadListData: action.payload.list,
      };
    },
    changeLoading(state, action) {
      return {
        ...state,
        loading: action.payload,
      };
    },
  }
};
